/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.i18n;

import com.genia.toolbox.web.gwt.basics.client.support.GwtObjectUtils;
import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * This class handles I18nMessage because GWT does not support Java5 yet.
 */
public class GwtI18nMessage
    implements IsSerializable
{
  /**
   * Returns a string representation of the object. In general, the
   * <code>toString</code> method returns a string that "textually represents"
   * this object. The result should be a concise but informative representation
   * that is easy for a person to read.
   * 
   * @return a string representation of the object
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    String res = key;
    for (int i = 0; i < parameters.length; i++) {
      res += ":" + parameters[i];
    }
    return res;
  }

  /**
   * the key of the message.
   */
  private String key;

  /**
   * the parameters of the message.
   */
  private String[] parameters = new String[0];



  /**
   * constructor.
   */
  public GwtI18nMessage()
  {
    this(null);
  }



  /**
   * constructor.
   * 
   * @param key
   *          the key of the message
   */
  public GwtI18nMessage(final String key)
  {
    this(key, new String[0]);
  }



  /**
   * constructor.
   * 
   * @param key
   *          the key of the message
   * @param parameters
   *          the parameters of the message
   */
  public GwtI18nMessage(final String key, String... parameters)
  {
    this.key = key;
    this.parameters = parameters;
  }



  /**
   * getter for the key property.
   * 
   * @return the key
   */
  public String getKey()
  {
    return key;
  }



  /**
   * setter for the key property.
   * 
   * @param key
   *          the key to set
   */
  public void setKey(final String key)
  {
    this.key = key;
  }



  /**
   * getter for the parameters property.
   * 
   * @return the parameters
   */
  public String[] getParameters()
  {
    return parameters;
  }



  /**
   * setter for the parameters property.
   * 
   * @param parameters
   *          the parameters to set
   */
  public void setParameters(final String... parameters)
  {
    this.parameters = parameters;
  }



  /**
   * calculate an hash code for this object.
   * 
   * @return a hash code for this object
   */
  @Override
  public int hashCode()
  {
    if (getKey() == null) {
      return 0;
    }
    return getKey().hashCode();
  }



  /**
   * returs wether this object is equals to another one.
   * 
   * @param other
   *          the other object
   * @return <code>true</code> if and only if this object is equals to the
   *         other one
   */
  @Override
  public boolean equals(final Object other)
  {
    if (other == null) {
      return false;
    }
    if (other == this) {
      return true;
    }
    if (other instanceof GwtI18nMessage) {
      final GwtI18nMessage otherMessage = (GwtI18nMessage) other;
      if (!GwtObjectUtils.nullSafeEquals(getKey(), otherMessage.getKey())) {
        return false;
      }
      if (getParameters().length != otherMessage.getParameters().length) {
        return false;
      }
      for (int i = 0; i < getParameters().length; i++) {
        if (!GwtObjectUtils.nullSafeEquals(getParameters()[i], otherMessage.getParameters()[i])) {
          return false;
        }
      }
      return true;
    }
    return false;
  }

}
